package com.neiru.pruebas.ar.renderer;

import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;
import android.opengl.GLU;

import com.neiru.pruebas.ar.figure.Cube;
import com.neiru.pruebas.ar.figure.Mesh;

public class HelloOpenGLES20Renderer implements GLSurfaceView.Renderer{
	
//	private Cube cube;
//	private Cube cube2;
//	private Cube cube3;
//	private Sphere sphere;
	public float[] rotates = {0, 0, -1};
	
	public List<Cube> figures;
	
	public HelloOpenGLES20Renderer(List<Cube> figures) {
		this.figures = figures;
//		cube = new Cube(1.0f, 1.0f, 1.0f,"blue");
//		cube2 = new Cube(1.0f, 1.0f, 1.0f,"red");
//		cube3 = new Cube(1.0f, 1.0f, 1.0f,"green");
//		sphere = new Sphere();
	}
    
	public void onDrawFrame(GL10 gl) {
		// Select the modelview matrix
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		// Reset the modelview matrix
		gl.glLoadIdentity();
		// Clears the screen and depth buffer.
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		//Posicion de la camara, direccion a la que mira y punto superiorz
		GLU.gluLookAt(gl, 0, 0, 0, rotates[0], rotates[1], rotates[2], 0, 1, 0);
		

		synchronized (figures) {
			List<Cube> toRemove = new ArrayList<Cube>();
			for(Cube figure: figures) {
				if(!figure.destroyed) {
					figure.draw(gl);
				} else {
					toRemove.add(figure);
				}
			}
			for(Cube delCube: toRemove) {
				figures.remove(delCube);
			}
		}
//		// Translates 4 units into the screen.
//		gl.glTranslatef(0, 0, -10); 
//		// Draw our square.
////		cube.draw(gl);
//		cube.draw(gl);
//		// volvemos al punto de inicio
//		gl.glTranslatef(0, 0, 10);
//
//		
//
//		
//		gl.glTranslatef(6, 0, 8);
//		gl.glRotatef(45f, 0, 1, 0);
//		cube2.draw(gl);
//		//volver al punto de inicio
//		gl.glRotatef(-45f, 0, 1, 0);
//		gl.glTranslatef(-6, 0, -8); 
//		
//		gl.glTranslatef(8,0,-6);
//		gl.glRotatef(45f,1,0,0);
//		cube3.draw(gl);
//		// VOlver al origen
//		gl.glRotatef(-45f,1,0,0);
//		gl.glTranslatef(-8,0,6);

		
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// Sets the current view port to the new size.
		gl.glViewport(0, 0, width, height);
		// Select the projection matrix
		gl.glMatrixMode(GL10.GL_PROJECTION);
		// Reset the projection matrix
		gl.glLoadIdentity();
		// Calculate the aspect ratio of the window
		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
				100.0f);
		//Posicion de la camara, direccion a la que mira y punto superiorz
//		GLU.gluLookAt(gl, 0, 0, 0, rotates[0], rotates[1], rotates[2], 0, 1, 0);
		// Select the modelview matrix
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		// Reset the modelview matrix
		gl.glLoadIdentity();
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// Set the background color to transparent( rgba ).
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		// Enable Smooth Shading, default not really needed.
		gl.glShadeModel(GL10.GL_SMOOTH);
		// Depth buffer setup.
		gl.glClearDepthf(1.0f);
		// Enables depth testing.
		gl.glEnable(GL10.GL_DEPTH_TEST);
		// The type of depth testing to do.
		gl.glDepthFunc(GL10.GL_LEQUAL);
		// Really nice perspective calculations.
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
        
	}
}
